Shader "ZHJ/doubleblur"
{
     Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags
        {
            "RenderPipeline" = "UniversalPipeline"
        }
        Cull Off ZWrite Off ZTest Always
        HLSLINCLUDE
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        #include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl"

        // CBUFFER_START(UnityPerMaterial)
        //     float4 _BlitTexture_ST;
        //     float _Blur;
        //     float4 _BlitTexture_TexelSize;
        // CBUFFER_END


        TEXTURE2D(_DitheringTexture);
        TEXTURE2D(_SourceTexture);
        // TEXTURE2D(_BlitTexture);
        // SAMPLER(sampler_BlitTexture);
        //
        // uniform float4 _BlitScaleBias;
        // uniform float2 _BlitTextureSize;
        uniform float _Blur;
        uniform float _BlurIntensity;
        
        uniform float _DitheringTextureInvSize;

        struct a2v
        {
            float4 positionOS : POSITION;
            float2 texcoord : TEXCOORD;
        };

        struct v2f
        {
            float4 positionCS : SV_POSITION;
            float4 texcoord[4] : TEXCOORD;
            float2 uv           :TEXCOORD5;
        };


        //----------------------------降采样-----------------------//
        v2f VERT(Attributes input)
        {
            Varyings output;
            UNITY_SETUP_INSTANCE_ID(input);
            UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

            v2f o;
            float4 pos = GetFullScreenTriangleVertexPosition(input.vertexID);
            float2 texcoord = GetFullScreenTriangleTexCoord(input.vertexID);
            o.positionCS = pos;


            texcoord = texcoord * _BlitScaleBias.xy + _BlitScaleBias.zw;

            o.texcoord[2].xy = texcoord;
            o.texcoord[0].xy = texcoord + float2(1, 1) * _BlitTextureSize.xy * (1 + _Blur) * 0.5;
            o.texcoord[0].zw = texcoord + float2(-1, 1) * _BlitTextureSize.xy * (1 + _Blur) * 0.5;
            o.texcoord[1].xy = texcoord + float2(1, -1) * _BlitTextureSize.xy * (1 + _Blur) * 0.5;
            o.texcoord[1].zw = texcoord + float2(-1, -1) * _BlitTextureSize.xy * (1 + _Blur) * 0.5;
            o.uv.xy = texcoord;
            return o;
        }

        half4 FRAG(v2f i):SV_TARGET
        {
            // half2 ditherUV = i.positionCS * _DitheringTextureInvSize;
            // half d = SAMPLE_TEXTURE2D(_DitheringTexture, sampler_PointRepeat, ditherUV).a;
            // d = d*2-1;
            // half2 ditherOffset = _BlitTextureSize.xy * half2(d,-d);
            half2 ditherOffset = 0; 

            half4 tex = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, i.texcoord[2].xy+ditherOffset) * 0.5;
            for (int t = 0; t < 2; t++)
            {
                tex += SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, i.texcoord[t].xy+ditherOffset) * 0.125;
                tex += SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, i.texcoord[t].zw+ditherOffset) * 0.125;
            }
            return tex;
        }

        //----------------------------降采样-----------------------//
        //----------------------------升采样-----------------------//

        v2f VERT1(Attributes input)
        {
            Varyings output;
            UNITY_SETUP_INSTANCE_ID(input);
            UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

            v2f o;
            float4 pos = GetFullScreenTriangleVertexPosition(input.vertexID);
            float2 texcoord = GetFullScreenTriangleTexCoord(input.vertexID);
            o.positionCS = pos;


            texcoord = texcoord * _BlitScaleBias.xy + _BlitScaleBias.zw;


            o.texcoord[0].xy =  float2(1, 1) * _BlitTextureSize.xy * (1 + _Blur) * 0.5;
            o.texcoord[0].zw =  float2(-1, 1) * _BlitTextureSize.xy * (1 + _Blur) * 0.5;
            o.texcoord[1].xy =  float2(1, -1) * _BlitTextureSize.xy * (1 + _Blur) * 0.5;
            o.texcoord[1].zw =  float2(-1, -1) * _BlitTextureSize.xy * (1 + _Blur) * 0.5;
            o.texcoord[2].xy =  float2(0, 2) * _BlitTextureSize.xy * (1 + _Blur) * 0.5;
            o.texcoord[2].zw =  float2(0, -2) * _BlitTextureSize.xy * (1 + _Blur) * 0.5;
            o.texcoord[3].xy =  float2(-2, 0) * _BlitTextureSize.xy * (1 + _Blur) * 0.5;
            o.texcoord[3].zw =  float2(2, 0) * _BlitTextureSize.xy * (1 + _Blur) * 0.5;
            o.uv.xy = texcoord;
            return o;
        }

        half4 FRAG1(v2f i):SV_TARGET
        {
            // half2 ditherUV = i.positionCS * _DitheringTextureInvSize;
            // half d = SAMPLE_TEXTURE2D(_DitheringTexture, sampler_PointRepeat, ditherUV).a;
            // d = d*2-1;
            // half2 ditherOffset = half2(-d,d)*_BlitTextureSize.xy;
            half2 ditherOffset = 0;
            half4 tex = 0;
            
            for (int t = 0; t < 2; t++)
            {
                tex += SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, i.uv+i.texcoord[t].xy+ditherOffset) / 6;
                tex += SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, i.uv+i.texcoord[t].zw-ditherOffset) / 6;
            }
            for (int t = 2; t < 4; t++)
            {
                tex += SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, i.uv+i.texcoord[t].xy+ditherOffset) / 12;
                tex += SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, i.uv+i.texcoord[t].zw-ditherOffset) / 12;
            }

            return tex;
        }


        half4 FinalFrag(v2f i):SV_TARGET
        {
            // half2 ditherUV = i.positionCS * _DitheringTextureInvSize;
            // half d = SAMPLE_TEXTURE2D(_DitheringTexture, sampler_PointRepeat, ditherUV).a;
            // d = d*2-1;
            // half2 ditherOffset = half2(-d,d)*_BlitTextureSize.xy;
            half2 ditherOffset = 0; 
            

            half4 tex = 0;
            for (int t = 0; t < 2; t++)
            {
                tex += SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, i.uv+i.texcoord[t].xy+ditherOffset) / 6;
                tex += SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, i.uv+i.texcoord[t].zw-ditherOffset) / 6;
            }
            for (int t = 2; t < 4; t++)
            {
                tex += SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, i.uv+i.texcoord[t].xy+ditherOffset) / 12;
                tex += SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, i.uv+i.texcoord[t].zw-ditherOffset) / 12;
            }

            // half4 sourceTex = SAMPLE_TEXTURE2D(_SourceTexture,sampler_LinearClamp,i.uv);

           // tex = lerp(sourceTex,tex,_BlurIntensity);
            
            
            return tex;
        }

        //----------------------------升采样-----------------------//
        ENDHLSL

        Pass
        {
            
            //DownSample
            ZWrite Off ZTest Always Blend Off Cull Off
            HLSLPROGRAM
            
            #pragma vertex VERT
            #pragma fragment FRAG
            ENDHLSL
        }
        Pass
        {

            
            //UpSample
            ZWrite Off ZTest Always Blend Off Cull Off
            HLSLPROGRAM
            #pragma vertex VERT1
            #pragma fragment FRAG1
            ENDHLSL
        }
        Pass
        {
            //FinalBlit
            ZWrite Off ZTest Always Blend Off Cull Off
            HLSLPROGRAM
            #pragma vertex VERT1
            #pragma fragment FinalFrag  
            ENDHLSL
        }

    }
}